package com.zzxx.service.impl;

import com.zzxx.entity.*;
import com.zzxx.mapper.*;
import com.zzxx.service.OrderService;
import com.zzxx.utils.QrCodeCreateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private TicketMapper ticketMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityScheduleMapper activityScheduleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired(required = false)
    private HttpServletRequest request;
    @Override
    public List<Orders> findByUserId(String bookerid) {

        return ordersMapper.findActivityByUid(bookerid);

//        OrdersExample ordersExample = new OrdersExample();
//        ordersExample.createCriteria().andBookeridEqualTo(bookerid);
//        return ordersMapper.selectByExample(ordersExample);
    }

    @Override
    public Orders findOrdersById(String id) {
        Orders orders = ordersMapper.selectByPrimaryKey(id);
        Ticket ticket = ticketMapper.selectByPrimaryKey(orders.getTicketid());
        Activity activity = activityMapper.selectByPrimaryKey(orders.getObjid());
        orders.setTicket(ticket);
        orders.setActivity(activity);
        return orders;
    }

    @Override
    public Integer updateById(String id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(1);
        return ordersMapper.updateByPrimaryKeySelective(orders);
    }

    @Override
    public Integer createdOrder(Orders orders) {
        return ordersMapper.insert(orders);
    }

    @Override
    public void createQr(Orders orders) {
        //生成uuid
        String uuid= UUID.randomUUID().toString().replace("-","");
        //生成核销码
        Integer hash=uuid.hashCode();
        hash=hash<0? -hash:hash;
        String hexHash=Integer.toHexString(hash);
        orders.setHexhash(hexHash);
        //生成二维码
        String parent = request.getServletContext().getRealPath("work/qrcreate")+"/"+new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String newImageName = uuid+".jpg";
        File qrImgFile = new File(parent,newImageName);
        if (!qrImgFile.getParentFile().exists()) {
            qrImgFile.getParentFile().mkdirs();
        }
        String fileName = qrImgFile.getAbsolutePath();
        QrCodeCreateUtils.CodeCreate(hexHash,500,500,fileName,"jpg");
        fileName = fileName.substring(fileName.indexOf("\\work"));
        orders.setQrsrc(fileName);
        //设置值
        orders.setId(uuid);
        orders.setStatus(0);
        orders.setCreatetime(new Date());
        orders.setObjtype(1);
        if ("doNotUseCoupon".equals(orders.getCouponid())) orders.setCouponid(null);
    }

    @Override
    public void updateType(String oid) {
        Orders orders = ordersMapper.selectByPrimaryKey(oid);
        orders.setContenttype(1);
        ordersMapper.updateByPrimaryKeySelective(orders);
    }

    @Override
    public void createActivitySchedule(ActivitySchedule schedule) {
        activityScheduleMapper.insert(schedule);
    }


}
